GridTooltip = Grid:NewModule("GridTooltip", "AceModuleCore-2.0", "AceHook-2.1")
local GridTooltip = GridTooltip

GridTooltip:SetModuleMixins("AceDebug-2.0")

GridTooltip.defaultDB = {
	hideOnLeave = true
}

GridTooltip.modulePrototype.core = GridTooltip

local surface = AceLibrary("Surface-1.0")

function GridTooltip.modulePrototype:OnInitialize()
	self.super.OnInitialize(self)
	
	if not self.db then
		self.core:RegisterDefaults(self.name, "profile", self.defaultDB or {})
		self.db = self.core:AcquireDBNamespace(self.name)
	end
	self.debugging = self.db.profile.debug
	self.debugFrame = GridTooltip.debugFrame
end

do
	local gfp
	function GridTooltip:OnInitialize()
		self.super.OnInitialize(self)
		
		gfp = GridFrame.frameClass.prototype

		local i = gfp.indicators
		local n = #i + 1

		self:Hook(gfp, "OnEnter", "GridFrameOnEnter")
		self:Hook(gfp, "OnLeave", "GridFrameOnLeave")
	end

	function GridTooltip:GridFrameOnEnter(frame)
		self:SetTooltip(frame.unit)
	end

	function GridTooltip:GridFrameOnLeave(frame)
		self:SetTooltip(nil)
	end
end

function GridTooltip:SetModuleEnabled(name, enable)
	enable = enable and true or false
	self.db.profile.modules[name] = enable
	self:ToggleModuleActive(name, enable)
end

function GridTooltip:EnableModules()
	local p = self.db.profile
	for name, module in self:IterateModules() do
		if p.modules[name] then
			self:ToggleModuleActive(module, true)
		end
	end
end

function GridTooltip:SetTooltip(unit)
	if not unit then
		if self.db.profile.hideOnLeave then
			self:HideTooltip()
		end
		return
	end
end

GridAlert.options = {
}

--[[
+-----------------------------+
| Name                * * * * |
| $=== life bar ===---$ -1432 |
| $=== mana bar ====--$  -123 |
+-----------------------------+
]]

function addon:CreateTipFrame()
	local p = self.db.profile
	local f = CreateFrame("Frame", nil, UIParent)
	local t, s, b
	
	f:SetPoint("BOTTOMLEFT", p.x, p.y)
	f:SetWidth(250)
	f:Setheight(60)
	
	f:SetBackdrop({ -- ripped of GridLayout
				 bgFile = "Interface\\ChatFrame\\ChatFrameBackground", tile = true, tileSize = 16,
				 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", edgeSize = 16,
				 insets = {left = 4, right = 4, top = 4, bottom = 4},
			 })

	t = f:CreateTexture(nil, "BORDER")
	t:SetTexture("Interface\\ChatFrame\\ChatFrameBackground")
	t:SetPoint("TOPLEFT", f, "TOPLEFT", 4, -4)
	t:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -4, 4)
	t:SetBlendMode("ADD")
	t:SetGradientAlpha("VERTICAL", .1, .1, .1, 0, .2, .2, .2, 0.5)
	-- f.backgroundTexture = t
	
	s = f:CreateFontString(nil, "ARTWORK")
	s:SetPoint("TOPLEFT")
	s:SetWidth(f:GetWidth())
	s:SetHeight(20)
	f.unitName = s
	
	local makeBar = function (offset, r, g, b)
		b = CreateFrame("StatusBar", nil, f)
		b:SetStatusBarTexture(surface:Fetch(self.db.profile.sbtexture))
		b:SetStatusBarColor(r, g, b)
		b:SetPoint("TOPLEFT", 0, -offset)
		b:SetWidth(180)
		b:SetHeight(10)
		
		local s = f:CreateFontString(nil, "ARTWORK")
		s:SetPoint("TOPLEFT", b, "TOPRIGHT", 10, 5)
		s:SetWidth(60)
		s:Setheight(20)
		s:SetTextColor(r, g, b)
		b.text = s
		
		return b
	end
	
	f.healthBar = makeBar(25, 0, 1, 0)
	f.manaBar = makeBar(45, ManaBarColor[0].r, ManaBarColor[0].g, ManaBarColor[0].b)
	
	addon.frame = f
end

function addon:InitializeHooks()
	self:Hook(GridFrame.frameClass.prototype, "OnEnter", "GridFrameOnEnter")
	self:Hook(GridFrame.frameClass.prototype, "OnLeave", "GridFrameOnLeave")
end

function addon:SetUnit(unit)
	if not unit and self.db.profile.hideOnLeave then
		self.frame:Hide()
	end
	self.frame:Show()
	self.unit = unit
	self:Update()
end

function addon:Update()
	local unit = self.unit
	local n = self.frame.unitName
	n:SetText(UnitName(unit))
	n:SetTextColor(BC:GetColor(UnitClass(unit)))
	local b = self.frame.healthBar
	local m, c = UnitHealthMax(unit), UnitHealth(unit)
	b:SetMinMaxValues(0, m)
	b:SetValue(c)
	b.text:SetString(L["%d / %d"]:format(c, m))
	
	b = self.frame.manaBar
	m, c = UnitManaMax(unit), UnitMana(unit)
	b:SetMinMaxValues(0, m)
	b:SetValue(c)
	b.text:SetString(L["%d / %d"]:format(c, m))
	local c = ManaBarColor[UnitPowerType(unit)]
	b:SetStatusBarColor(info.r, info.g, info.b)
	b.text:SetTextColor(info.r, info.g, info.b)
end

do
	local events = {
		UNIT_DISPLAYPOWER = "UpdateUnit",
		UNIT_ENERGY = "UpdateUnit",
		UNIT_FOCUS = "UpdateUnit",
		UNIT_MANA = "UpdateUnit",
		UNIT_HEALTH = "UpdateUnit",
		UNIT_MAXENERGY = "UpdateUnit",
		UNIT_MAXFOCUS = "UpdateUnit",
		UNIT_MAXHEALTH = "UpdateUnit",
		UNIT_MAXMANA = "UpdateUnit",
		UNIT_MAXRAGE = "UpdateUnit",
		UNIT_NAME_UPDATE = "UpdateUnit",
		UNIT_RAGE = "UpdateUnit",
	}

	function addon:RegisterEvents()
		for event, method in pairs(events) do
			self:RegisterEvent(event, method)
		end
	end
end

function addon:UpdateUnit(unit)
	if unit and UnitIsUnit(self.unit, unit) and self.frame:IsShown() then
		self:Update()
	end
end
